#include "clientthread.h"
#include <QtNetwork/QHostAddress>

ClientThread::ClientThread(QObject *parent) :
    QThread(parent)
{
}
ClientThread::ClientThread(int fd,QDomDocument userList)
{
    m_ClientConnection.setSocketDescriptor(fd);
    m_userList = userList;

}

void ClientThread::run()
{

    QObject::connect(&m_ClientConnection,SIGNAL(connected()),this,SLOT(processConnected()));
    QObject::connect(&m_ClientConnection,SIGNAL(disconnected()),this,SLOT(processDisconnected()));
    QObject::connect(&m_ClientConnection,SIGNAL(readyRead()),this,SLOT(processReadyRead()));

    m_UID = tr("20082075");
    m_NickName = tr("yaoboyuan");
    m_IP = m_ClientConnection.peerAddress().toString();
    m_Port = m_ClientConnection.peerPort();

    ///tell the wiget that new connection has come
    emit NewConnection(m_UID,m_NickName,m_IP,m_Port);

    MSGHeader header;
    QByteArray data(m_userList.toByteArray());
    header.len = data.length();
    header.operation = RESPOND;
    header.target = YES;

    QByteArray package = generatePackage(header,data);
    sendPackage(package);

    this->exec();
}
void ClientThread::processConnected()
{
    ///do nothing
}
void ClientThread::processDisconnected()
{
    emit DisConnection(m_UID,m_NickName,m_IP,m_Port);
    m_ClientConnection.close();
    exit(0);
}
void ClientThread::processReadyRead()
{
    MSGHeader header;
    QByteArray data;

    QByteArray h;
    h = m_ClientConnection.read(sizeof(header));
    memcpy(&header,h.data(),sizeof(header));

    data = m_ClientConnection.read(header.len);
    interpretOperation(header,data);
}
void ClientThread::sendPackage(QByteArray package)
{
    m_ClientConnection.write(package);
}
void ClientThread::processServerClose()
{
    m_ClientConnection.close();
    exit(0);
}

///===============client action process zone=========================
void ClientThread::interpretOperation(MSGHeader header,QByteArray data)
{
    //========test===========================
    //QByteArray package;
    //package = generatePackage(header,data);
    //m_ClientConnection.write(package);

    return ;
    //=========test end======================
    switch (header.operation)
    {
    case LOGIN:
        processLogin(header,data);
        break;
    case ADD:
        processAdd(header,data);
        break;
    case DELETE:
        processDelete(header,data);
        break;
    case UPDATE:
        processUpdate(header,data);
        break;
    case BROWSE:
        processBrowse(header,data);
        break;
    default:
        processBrowse(header,data);
        break;
    }
}
void ClientThread::processRegister(const MSGHeader &header,const QByteArray &data)
{
    ///check the user,and respond to client

    ///emit responseRegister(data)
}
void ClientThread::processLogin(const MSGHeader &header,const QByteArray &data)
{
    ///check the user,and respond to client

    ///emit responseLogin(data)
}
void ClientThread::processBrowse(const MSGHeader &header, const QByteArray &data)
{
    ///get the client's browse request,and query the database

    ///emit responseBrowse(data),response to client
}

void ClientThread::processAdd(const MSGHeader &header,const QByteArray &data)
{
    switch (header.target)
    {
    case CALENDAR:
        break;
    case IDEABOX:
        break;
    case TODOLIST:
        break;
    }
}
void ClientThread::processDelete(const MSGHeader &header,const QByteArray &data)
{
    switch (header.target)
    {
    case CALENDAR:
        break;
    case IDEABOX:
        break;
    case TODOLIST:
        break;
    case INFO:
        break;
    }
}
void ClientThread::processUpdate(const MSGHeader &header,const QByteArray &data)
{
    switch (header.target)
    {
    case CALENDAR:
        break;
    case IDEABOX:
        break;
    case TODOLIST:
        break;
    case INFO:
        break;
    case PASSWORD:
        break;
    }
}
///=====================end process==================================
